package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import net.miginfocom.swing.MigLayout;
import dto.LogonDTO;

public class MainMenuPage {

	private JPanel panel;
	private JFrame frame;
	private JTextField txtUsername;
	private JTextField txtLevel;

	private JButton btnAddUser = new JButton("Add User");
	private JButton btnEditUser = new JButton("Edit/Delete User");
	private JButton btnAddBook = new JButton("Add Book");
	private JButton btnEditBook = new JButton("Edit/Delete Book");
	private JButton btnLendBook = new JButton("Borrow/Return Book");
	private JButton btnSearch = new JButton("Search for Book");

	private LogonDTO logonDTO;

	/**
	 * Create the application.
	 */
	public MainMenuPage(LogonDTO logonDTO) {

		this.logonDTO = logonDTO;

		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 450, 356);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new MigLayout("", "[grow][][][][][][]", "[][][][]"));

		drawTopPanel(panel);

		// **************************************************************

		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new MigLayout("", "[grow][][grow]", "[][]"));

		JButton btnChangeMyPassword = new JButton("Change my password");
		if (logonDTO.getLevel() < 1) {
			btnChangeMyPassword.setEnabled(false);
		} else {
			btnChangeMyPassword.setEnabled(true);
		}
		panel_1.add(btnChangeMyPassword, "cell 0 0");

		JButton btnLogOff = new JButton("Log off");
		btnLogOff.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				drawTopPanel(panel);
				showUserCredentials();
			}
		});
		panel_1.add(btnLogOff, "cell 2 0,alignx right");

		txtUsername = new JTextField();
		txtUsername.setEditable(false);
		txtUsername.setText("guest");
		panel_1.add(txtUsername, "cell 0 1,growx");
		txtUsername.setColumns(10);

		txtLevel = new JTextField();
		txtLevel.setEditable(false);
		txtLevel.setHorizontalAlignment(SwingConstants.RIGHT);
		txtLevel.setText("Guest");
		panel_1.add(txtLevel, "cell 2 1,growx");
		txtLevel.setColumns(10);

		showUserCredentials();

		frame.setVisible(true);
	}

	private void showUserCredentials() {

		txtUsername.setText(logonDTO.getUserName());
		int level = logonDTO.getLevel();

		String editorLevel = null;

		switch (level) {
		case 0:
			editorLevel = UserLevelEnum.GUEST.getUserTitle();
			break;
		case 1:
			editorLevel = UserLevelEnum.ADMIN.getUserTitle();
			break;
		case 2:
			editorLevel = UserLevelEnum.LIBRARIAN.getUserTitle();
			break;
		case 3:
			editorLevel = UserLevelEnum.MEMBER.getUserTitle();
			break;
		}

		txtLevel.setText(editorLevel);
		logonDTO.setUserTitle(editorLevel);
	}

	public void drawTopPanel(JPanel panel) {

		if (panel.getComponentCount() > 0) {
			panel.removeAll();
			System.out.println("Removed all");
		}

		JLabel lblJaLibrary = new JLabel("J&A Library");
		lblJaLibrary.setForeground(Color.BLUE);
		lblJaLibrary.setFont(new Font("Arial", Font.BOLD, 16));
		panel.add(lblJaLibrary, "flowy,cell 0 0 6 1,alignx center");

		JLabel lblMainMenu = new JLabel("Main Menu");
		panel.add(lblMainMenu, "cell 0 1 6 1,alignx center");

		JLabel label = new JLabel("                    ");
		panel.add(label, "cell 0 2");

		// create options for non-guest
		int counter = drawOptions();

		JLabel labelIndexForSearch = new JLabel(counter + ".");
		panel.add(labelIndexForSearch, "flowx,cell 0 " + (counter + 2));

		panel.add(btnSearch, "cell 0 " + (counter + 2) + " 6 1");

		panel.revalidate();
		panel.repaint();
	}

	public int drawOptions() {

		int counter = 1;
		int userLevel = logonDTO.getLevel();

		// ADD USER
		if ((userLevel == 1) || (userLevel == 2)) {
			JLabel labelIndexForAddUser = new JLabel(counter + ".");
			panel.add(labelIndexForAddUser, "flowx,cell 0 " + (counter + 2));
			panel.add(btnAddUser, "cell 0 " + (counter + 2) + " 6 1");
			counter++;
		}

		// EDIT/DELETE USER
		if ((userLevel == 1) || (userLevel == 2)) {
			JLabel labelIndexForEditUser = new JLabel(counter + ".");
			panel.add(labelIndexForEditUser, "flowx,cell 0 " + (counter + 2));
			panel.add(btnEditUser, "cell 0 " + (counter + 2) + " 6 1");
			counter++;
		}

		// ADD BOOK
		if ((userLevel == 1) || (userLevel == 2)) {
			JLabel labelIndexForAddBook = new JLabel(counter + ".");
			panel.add(labelIndexForAddBook, "flowx,cell 0 " + (counter + 2));
			panel.add(btnAddBook, "cell 0 " + (counter + 2) + " 6 1");
			counter++;
		}

		// EDIT/DELETE BOOK
		if ((userLevel == 1) || (userLevel == 2)) {
			JLabel labelIndexForEditBook = new JLabel(counter + ".");
			panel.add(labelIndexForEditBook, "flowx,cell 0 " + (counter + 2));

			panel.add(btnEditBook, "cell 0 " + (counter + 2) + " 6 1");
			counter++;
		}

		// BORROW/RETURN BOOK
		if ((userLevel == 1) || (userLevel == 2)) {
			JLabel labelIndexForLendBook = new JLabel(counter + ".");
			panel.add(labelIndexForLendBook, "flowx,cell 0 " + (counter + 2));

			panel.add(btnLendBook, "cell 0 " + (counter + 2) + " 6 1");
			counter++;
		}

		return counter;
	}

	public void addAddUserButtonListener(ActionListener al) {
		btnAddUser.addActionListener(al);
	}

	public void addEditDelUserButtonListener(ActionListener al) {
		btnEditUser.addActionListener(al);
	}

	public void addAddBookButtonListener(ActionListener al) {
		btnAddBook.addActionListener(al);
	}

	public void addEditDelBookButtonListener(ActionListener al) {
		btnEditBook.addActionListener(al);
	}

	public void addBorrowReturnBookButtonListener(ActionListener al) {
		btnLendBook.addActionListener(al);
	}

	public void addSearchForBookButtonListener(ActionListener al) {
		btnSearch.addActionListener(al);
	}

}
